var listUrl = rootPath+'/manage/markingPaper/objective';//列表
var markingHandleUrl = rootPath+'/manage/markingPaper/objective/markingHandle/exec';//阅卷处理
var viewScoreUrl = rootPath+'/manage/markingPaper/objective/viewObjectiveStudentUI';//查看考生分数
var paperHandleUrl =  rootPath+'/manage/markingPaper/objective/getHandleSession';//阅卷进度

var star='★ ';
var destory_value = 0;
var progressbar = $("#progressbar"),progressLabel = $(".progress-label");
$(function(){
	queryGradeByBatch();
	$('#query_batch').change(function(){
		queryGradeByBatch();
	});
	$('#query_grade').change(function(){
		queryCourseByGrade();
	});
	
	$('.btn-start').click(function(){
		handleProgress($(this).parents('tr').attr('data-tempId'),1);
	});
	$('.btn-restart').click(function(){
		handleProgress($(this).parents('tr').attr('data-tempId'),2);
	});
	$('.btn-view').click(function(){
		viewObjectiveScore($(this).parents('tr').attr('data-tempId'));
	});
	initBar();
});

//根据批次查询年级
var queryGradeByBatch = function(){
	common_ajax.ajax(rootPath+'/manage/courseTemplate/queryGradeByBatch', {batchId : $('#query_batch option:selected').val()}, 'json', function(data){
		if(data.list){
			var html = '<option value="">---请选择---</option>';
			$(data.list).each(function(index, item){
				html += '<option value="'+item.gradeNo+'"';
				if(item.gradeNo==query_grade){
					html += ' selected="selected"';
				}
				html += '>'+item.gradeName+'</option>';
			});
			$('#query_grade').html(html);
			queryCourseByGrade();
		}
	});
}
//根据年级查询课程
var queryCourseByGrade = function(){
	var batchId = $('#query_batch option:selected').val();
	var gradeNo = $('#query_grade option:selected').val();
	if(query_grade != null && query_course != null){
		batchId = query_grade;
		gradeNo = query_course;
	}
	common_ajax.ajax(rootPath+'/manage/courseTemplate/queryCourseByGrade', {batchId : batchId,gradeNo : gradeNo}, 'json', function(data){
		if(data.list){
			var html = '<option value="">---请选择---</option>';
			$(data.list).each(function(index, item){
				html += '<option value="'+item.courseNo+'"';
				if(item.courseNo==query_course){
					html += ' selected="selected"';
				}
				html += '>'+item.courseName+'</option>';
			});
			$('#query_course').html(html);
		}
	});
}

//刷新事件
function refreshBack(){
	location.href = listUrl;
}

//查看考生分数
function viewObjectiveScore(tempId){
	layer.open({
		title : '查看客观题考生分数',
		type : 2,
		area : [ '800px', '600px' ],
		content : viewScoreUrl + '?tempId='+tempId
	});
}
/**************进度条部分****************/
function initBar(){
	progressbar.progressbar({
		value: false,
		change: function() {
	  		progressLabel.text(progressbar.progressbar("value") + "%");
		},
		complete: function() {
	  		progressLabel.text( "客观题阅卷完成!" );
		}
	});
}

//处理进度条
function handleProgress(tempId,type){
	var message = '您确定要进行阅卷吗，是否继续？';
	if(type==2){//重新阅卷
		message = '您确定要进行重新阅卷吗，是否继续？'
	}
	layer.confirm(message, function(index) {
		$.fx.speeds._default = 1000;//页面加载事件，数值越大加载越慢
		$('#dialog').dialog({
			title: '答卷客观题自动阅卷进度条',
			autoOpen: false,
			show: "blind",
			hide: "slow",
			height: 150,
			width: 800,
			modal: true,
			draggable: false,
			closeOnEscape: false
		});
		$('#dialog').dialog("open");
		destory_value =0;
		setTimeout(progress, 1000);
		common_ajax.ajax(markingHandleUrl, {courseTemplateId : tempId}, 'json');
		//initBar();
		//progress();//启动进度条
	});
}

var progress = function() {
	//获取处理进度数据信息
	common_ajax.ajax(paperHandleUrl, {}, 'json', function(data){
		var data_ = eval(data.data);
		var bl = data_.bl;
		if(bl=='0' || bl==''){
			$('#progress-show-text').html("正在执行阅卷，请勿关闭或刷新页面......");
		}else{
			var djzs = data_.djzs;
			var clzs = data_.clzs;
			if(clzs > djzs){
				clzs = djzs;
			}
			$('#progress-show-text').html("当前进度 "+clzs+"/"+djzs);
		}
		
		if(destory_value == 0){
			progressbar.progressbar("value", bl);
		}else{
			initBar();
		}
		
		var resultCode = data.resultCode;
    	if (bl < 100 && (resultCode !=null && resultCode=='' || typeof(resultCode) == "undefined")) {
      		setTimeout(progress, 2000);
    	}
    	
    	if(resultCode !=null && resultCode!='' && typeof(resultCode) != "undefined"){
    		resultShow(resultCode,data_);
    	}
	});
}

//答卷处理结果展示
function resultShow(resultCode,data){
	destory_value = 1;
	$('#dialog').dialog("close");
	$('#dialog').empty();
	if(resultCode=='000000'){//★☆◆◇■
		var str='<span style="line-height: 25px;display: inline-block;">'+star;
		var message='客观题阅卷完成，记录总数【'+data.djzs+'】<br>'+str+'成功数【'+data.succCount+'】<br>'+str+'失败数【'+data.failCount+'】';
		layer.alert(message,{icon:1},function(index){
			layer.close(index);
			refreshBack();
		});
	}else if(resultCode=='000021'){
		layer.alert('对不起，未查询到设置的客观题数据，请联系管理员检查模板设置！',{icon: 5});
		return false;
	}else if(resultCode=='000022'){
		layer.alert('当前客观题未设置答案数据，请先设置客观题答案！',{icon: 5});
		return false;
	}else{
		layer.alert('操作失败！',{icon: 5});
		return false;
	}
}